package com.dcbeida.mine;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.addnewpatient.AddNewPatientAct;
import com.dcbeida.activity.sales.StoresalesActivity;
import com.dcbeida.activity.sign.SignQrCodeActivity;
import com.dcbeida.adapter.MyClientAdapter;
import com.dcbeida.adapter.PatientListAdapter;
import com.dcbeida.bean.MyClientBean;
import com.dcbeida.bean.Patient;
import com.dcbeida.bean.SourceBean;
import com.dcbeida.listern.DialogListener;
import com.dcbeida.mine.detail.MyClientDetailAct;
import com.dcbeida.utils.DialogUtils;
import com.dcbeida.widget.DataLoadDirection;
import com.dcbeida.widget.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

import static com.dcbeida.utils.Constants.RESULT_PATIENT_CODE;

/**
 * 我的客户
 * Created by 150410 on 2017/9/18.
 */

public class PatientListActivity extends BaseActivity implements XListView.IXListViewListener, PatientListContract.View, AdapterView.OnItemClickListener {
    @Bind(R.id.img_back)
    ImageView imgBack;


    @Bind(R.id.myList)
    XListView myList;
    @Bind(R.id.ll_no_data)
    LinearLayout llNoData;

    @Bind(R.id.tv_add_patient)
    TextView tv_add_patient;

    private List<Object> datalist;
    private PatientListAdapter adapter;
    private PatientListPersenter mPresent;

    private boolean isEnd = false;// 是否有更多
    private int Totle = 0;//
    private int pageindex = 0;
    private int pagesize = 10;

    int type = DataLoadDirection.Refresh;
    private String str;

    private int searchType;

    private String keyWord;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_patient_list;
    }

    @Override
    public void setData() {

        mPresent = new PatientListPersenter(this, this);
        Bundle bundle = getIntent().getExtras();

        imgBack.setVisibility(View.VISIBLE);


        datalist = new ArrayList<Object>();
        myList.setPullLoadEnable(false);
        myList.setPullRefreshEnable(true);
        myList.setXListViewListener(this);
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        myList.setOnItemClickListener(this);
        adapter = new PatientListAdapter(this);
        myList.setAdapter(adapter);

        if (null != bundle) {
            searchType = bundle.getInt("searchType");
            keyWord = bundle.getString("keyWord");
        }


    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresent.getPatientInfo(searchType,keyWord);
    }

    @OnClick({R.id.img_back,R.id.tv_add_patient})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_add_patient:
                showActivity(AddNewPatientAct.class, null);
//                Bundle bundle = new Bundle();
//                bundle.putInt("patientId",195417);
//                bundle.putInt("type",0);
//                showActivity(SignQrCodeActivity.class, bundle);
                break;
        }
    }

    @Override
    public void onRefresh() {
        // 执行刷新任务
        type = DataLoadDirection.Refresh;
        mPresent.getPatientInfo(searchType,keyWord);
    }

    @Override
    public void onLoadMore() {
        // 执行加载更多任务

    }



    @Override
    public void setPresenter(PatientListContract.Persent presenter) {

    }

    @Override
    public void showTelTip(String msg) {
        showTip(msg);
    }

    @Override
    public void showView(boolean hasData) {
        if (hasData) {
            myList.setVisibility(View.VISIBLE);
            llNoData.setVisibility(View.GONE);
        } else {
            myList.setVisibility(View.GONE);
            llNoData.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showPatientlist(ArrayList<Patient> patients) {
       // if (type == DataLoadDirection.Refresh) {
            myList.stopRefresh();
            pageindex = 0;
            adapter.clearList();
        //}
        adapter.setmArrayList(patients);
        if (adapter.getCount()>0) {
            myList.setVisibility(View.VISIBLE);
            llNoData.setVisibility(View.GONE);
        } else {
            myList.setVisibility(View.GONE);
            llNoData.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        Patient patient = (Patient) adapter.getItem(position - 1);
        bundle.putSerializable("patient", patient);
        intent.putExtras(bundle);
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    public void showAddNewPatient() {
        showPatientlist(new ArrayList<Patient>());
        //根据患者姓名未查到患者，显示添加弹窗
//        DialogUtils.showDialogAddNewPatient(PatientListActivity.this, R.string.wzdghz, new DialogListener() {
//            @Override
//            public void onCancl(Dialog dialog) {
//                dialog.dismiss();
//            }
//
//            @Override
//            public void onConfirm(Dialog dialog) {
//                dialog.dismiss();
//                showActivity(AddNewPatientAct.class, null);
//            }
//        }, R.string.cancel, R.string.xzhz);

    }

}
